package edu.spec.student.controller;

import edu.spec.student.model.Course;
import edu.spec.student.service.iface.ICourseService;
import edu.spec.basic.model.User;
import edu.spec.basic.model.dto.MvcReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by MadFrog on 2017/10/13.
 * 课程管理 控制器
 * 管理人员（教师）使用
 */
@Controller
@RequestMapping("/admin/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    // 增加课程
    @ResponseBody
    @RequestMapping("/add")
    public MvcReturn add(Course c){
        courseService.add(c);
        return new MvcReturn(true);
    }

    // 删除课程     慎用
    @ResponseBody
    @RequestMapping("/delete/{id}")
    public MvcReturn delete(@PathVariable Integer id){
        courseService.delete(id);
        return new MvcReturn(true);
    }

    // 修改课程
    @ResponseBody
    @RequestMapping("/update")
    public MvcReturn update(Course c){
        courseService.update(c);
        return new MvcReturn(true);
    }

    // 查询课程
    @ResponseBody
    @RequestMapping("/listByTeacher")
    public MvcReturn listByTeacher(HttpSession session){
        User u = (User)session.getAttribute("user");
        List<Course> lc = courseService.listCourseByTeacher(u.getId());
        return new MvcReturn(true, lc);
    }

}
